package com.github.cbyzzy.service;

/**
 * @Name: AlarmService
 * @Author: HenryWong 549264639@qq.com
 * @Date: 2024-12-12 18:27
 */

import com.github.cbyzzy.common.FMSGCallBack_V31;
import com.github.cbyzzy.hik.HCNetSDK;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AlarmService {

    private static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    private HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();//设备登录信息
    private HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();//设备信息

    private HCNetSDK.FMSGCallBack fMSFCallBack;//报警回调函数实现
    private FMSGCallBack_V31 fMSFCallBack_V31;//报警回调函数实现

    private HCNetSDK.FColGlobalDataCallBack fGpsCallBack;//GPS信息查询回调函数实现

    // 初始化HCNetSDK以及相关设置的方法
    public void initSDKAndSettings() {
        if (!hCNetSDK.NET_DVR_Init()) {
            log.error("SDK初始化失败");
            return;
        }
        log.info("SDK初始化成功");
        //设置连接超时时间与重连功能
        hCNetSDK.NET_DVR_SetConnectTime(2000, 1);
        hCNetSDK.NET_DVR_SetReconnect(10000, true);
    }

    // 设备登录的方法
    public int loginDevice(String ip, short port, String name, String password) {
        HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        int lUserID = hCNetSDK.NET_DVR_Login_V30(ip, port, name, password, m_strDeviceInfo);
        if (lUserID < 0) {
            log.error("Login failed, error code={}", hCNetSDK.NET_DVR_GetLastError());
        } else {
            log.info("设备报警注册成功");
        }
        return lUserID;
    }

    // 设置报警回调函数的方法
    public void setAlarmCallback() {
        FMSGCallBack_V31 fmsgCallBack_v31 = new FMSGCallBack_V31();
        boolean b = hCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(fmsgCallBack_v31, null);
        if (!b) {
            log.error("SetDVRMessageCallBack failed, error code={}", hCNetSDK.NET_DVR_GetLastError());
        }
    }

    // 建立报警上传通道（布防）的方法
    public int setupAlarmChan(int lUserID) {
        HCNetSDK.NET_DVR_SETUPALARM_PARAM net_dvr_setupalarm_param = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
        int nativeLong = hCNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID, net_dvr_setupalarm_param);
        if (nativeLong < 0) {
            log.error("SetupAlarmChan failed, error code={}", hCNetSDK.NET_DVR_GetLastError());
            // 这里可以根据实际需求决定是否立即执行注销和资源释放操作，示例中暂不处理
             hCNetSDK.NET_DVR_Logout(lUserID);
             hCNetSDK.NET_DVR_Cleanup();
        }
        return nativeLong;
    }
}
